using UnityEngine;

public class ButtonJump : MonoBehaviour
{
	private ThirdPersonController cacheController;

	private float timeDelay = 0.35f;

	private bool onJetpack;

	private float timeOnJetpack;

	private bool _needDisable;

	private void OnPress(bool isDown)
	{
		CameraZoneEnabler.GetCameraZone().OnPress(isDown);
		if (GameController.instance.playerScript.isShock)
		{
			return;
		}
		if (GameController.instance.playerScript.isDead)
		{
			isDown = false;
		}
		CheckCache();
		if (isDown)
		{
			cacheController.lastJumpButtonTime = Time.time;
			if (settings.jetpackFuel > 0f)
			{
				if (cacheController.IsGroundedWithTimeout())
				{
					Invoke("ActivateJetpack", timeDelay);
				}
				else
				{
					ActivateJetpack();
				}
				_needDisable = true;
			}
			if (!cacheController.jumping)
			{
				cacheController.jumping = true;
				AllUnitaryAchievements.instance.jump_1000.Increment();
			}
		}
		else
		{
			CancelInvoke("ActivateJetpack");
			if (!settings.offlineMode && _needDisable)
			{
				cacheController.pBehavior.DisableJetpackParticle();
				_needDisable = false;
			}
			cacheController.jetpack.isFlying = false;
			Invoke("StopJetpackTimer", 3f);
		}
	}

	private void StopJetpackTimer()
	{
		CancelInvoke("StopJetpackTimer");
		if (timeOnJetpack > 1f)
		{
			AllUnitaryAchievements.instance.flyOnJetpack_1800sec.Increment((int)timeOnJetpack);
		}
		timeOnJetpack = 0f;
		onJetpack = false;
	}

	private void OnDisable()
	{
		CameraZoneEnabler.GetCameraZone().OnPress(false);
		StopJetpackTimer();
	}

	private void Update()
	{
		if (!onJetpack)
		{
			return;
		}
		if (timeOnJetpack <= 30f)
		{
			timeOnJetpack += Time.deltaTime;
		}
		else
		{
			if (timeOnJetpack > 1f)
			{
				AllUnitaryAchievements.instance.flyOnJetpack_1800sec.Increment((int)timeOnJetpack);
			}
			timeOnJetpack = 0f;
		}
		if (cacheController.pBehavior.isGrounded)
		{
			StopJetpackTimer();
		}
	}

	private void ActivateJetpack()
	{
		CancelInvoke("StopJetpackTimer");
		onJetpack = true;
		if (!settings.offlineMode)
		{
			CheckCache();
			cacheController.pBehavior.ActivateJetpack();
		}
		else
		{
			CheckCache();
			cacheController.jetpack.Activate();
		}
		if (!cacheController.jumping)
		{
			cacheController.jumping = true;
		}
		if (cacheController.jetpack.activated)
		{
			if (!settings.offlineMode)
			{
				cacheController.pBehavior.EnableJetpackParticle();
			}
			if (cacheController.jumping)
			{
				cacheController.jetpack.isFlying = true;
				cacheController.pBehavior.alreadyDontFly();
			}
		}
	}

	private void CheckCache()
	{
		if (cacheController == null)
		{
			cacheController = GameController.instance.myPlayer.GetComponent<ThirdPersonController>();
		}
	}
}
